//
//  CacheHelper.m
//  ChangChat
//
//  Created by Cocoa on 2019/8/29.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "CacheHelper.h"
#import <LKDBHelper/LKDBHelper.h>

@implementation CacheHelper

+ (instancetype)helper {
    static CacheHelper *_cacheHelper = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _cacheHelper = [[CacheHelper alloc] init];
    });
    return _cacheHelper;
}

- (void)cacheFileWitType:(MsgType)type fileData:(NSData *)data completion:(void(^)(NSString *filePath))completion {
    
    NSString *fileName = [self newFileNameForMsgType:type];
    NSString *filePath = [self allFilePathWithMsgType:type fileName:fileName];
    
    [data writeToFile:filePath atomically:YES];
    if (completion) {
        completion(fileName);
    }
}

- (NSString *)newFileNameForMsgType:(MsgType)msgType {
    NSDate *date = [[NSDate alloc]init];
    NSDateFormatter *dateFormate = [[NSDateFormatter alloc]init];
    dateFormate.dateFormat = @"yyyy-MM-dd' 'HH:mm:ss.SSS";
    NSString *dateStr = [dateFormate stringFromDate:date];
    NSString *_fileName = nil;
    switch (msgType) {
            
        case MsgType_Video: {
                _fileName = [NSString stringWithFormat:@"%@_video.mp4", dateStr];
            break;
        }
        case MsgType_Voice: {
                _fileName = [NSString stringWithFormat:@"%@_voice.caf", dateStr];
            break;
        }
        case MsgType_File: {
                _fileName = [NSString stringWithFormat:@"%@_file.dat", dateStr];
        }
            break;
        case MsgType_Photo: {
            return [dateStr stringByAppendingString:@"_photo"];
        }
            break;
        default:
            break;
    }
    return _fileName;
}
- (NSString *)allFilePathWithMsgType:(MsgType)msgType fileName:(NSString * _Nullable)fileName {
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:kUsermanager.me.ID?:@"0"];
    NSString *allPath = nil;
    switch (msgType) {
        case MsgType_Video: {
            allPath = [documentPath stringByAppendingPathComponent:@"msg_video/"];
        }break;
        case MsgType_Voice: {
            allPath = [documentPath stringByAppendingPathComponent:@"msg_voice/"];
        }break;
        case MsgType_File: {
            allPath = [documentPath stringByAppendingPathComponent:@"msg_file/"];
        }break;
            default:
            break;
    }
    [self createFilePathIfNeed:allPath];
    allPath = [allPath stringByAppendingPathComponent:fileName];
    return allPath;
}
- (NSString *)filePathWithMsgType:(MsgType)msgType {
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:kUsermanager.me.ID?:@"0"];
    NSString *fillPath = nil;
    switch (msgType) {
        case MsgType_Video: {
            fillPath = [documentPath stringByAppendingPathComponent:@"msg_video/"];
        }break;
        case MsgType_Voice: {
            fillPath = [documentPath stringByAppendingPathComponent:@"msg_voice/"];
        }break;
        case MsgType_File: {
            fillPath = [documentPath stringByAppendingPathComponent:@"msg_file/"];
        }break;
        default:
            break;
    }
    [self createFilePathIfNeed:fillPath];
    return fillPath;
}

- (void)createFilePathIfNeed:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path] == NO) {
        [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }
}

- (NSArray *)allFileName {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *filePath = [self filePathWithMsgType:MsgType_File];
    if ([fileManager fileExistsAtPath:filePath] == NO) {
        return nil;
    }
    NSArray *filePathArr = [fileManager subpathsAtPath:filePath];
    return filePathArr;
}


- (NSString *)fileSizeWithFileName:(NSString *)fileName {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *filePath = [self allFilePathWithMsgType:MsgType_File fileName:fileName];
    if ([fileManager fileExistsAtPath:filePath]){
        NSDictionary *dic = [fileManager attributesOfItemAtPath:filePath error:nil];
        CGFloat size = [dic fileSize]/(1024.0*1024.0);
        return [NSString stringWithFormat:@"%.2fM", size];
    }
    return @"0.0M";
}
- (void)cleanFilesWithGroupId:(NSString *)groupId {
    NSArray *fileMsgArr = [MsgItem searchWithWhere:@{@"group_id": groupId, @"type":@[@"2",@"3",@"4"]}];
    for (MsgItem *msg in fileMsgArr) {
        if (msg.filePath.isHaveStr) {
            NSString *filePath = [self allFilePathWithMsgType:msg.type fileName:msg.filePath];
            [FileUtils deleteFileWithFullPath:filePath];
        }
    }
}
- (void)cleanAllFiles {
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:kUsermanager.me.ID?:@"0"];
    [FileUtils deleteFileWithFullPath:filePath];
}
@end
